package com.itwlj.service.impl;

import com.itwlj.entity.Banner;
import com.itwlj.mapper.BannerMapper;
import com.itwlj.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 轮播图业务层
 */
@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    /**
     * 新增轮播图
     *
     * @param banner
     */
    @CacheEvict(value = "banners", allEntries = true)
    public void insert(Banner banner) {
        bannerMapper.insert(banner);
    }

    /**
     * 查询轮播图
     *
     * @return
     */
    @Cacheable(value = "banners", key = "'bannerList'")
    public List<Banner> list() {
        return bannerMapper.list();
    }

    /**
     * 根据id删除轮播图
     *
     * @param id
     */
    @CacheEvict(value = "banners", allEntries = true)
    public void deleteById(Long id) {
        bannerMapper.deleteById(id);
    }

    /**
     * 根据id修改轮播图
     *
     * @param banner
     */
    @CacheEvict(value = "banners", allEntries = true)
    public void update(Banner banner) {
        bannerMapper.update(banner);
    }

    /**
     * 根据分发站点查询轮播图
     *
     * @param distributionSite
     * @return
     */
    @Cacheable(value = "banners", key = "'bannerList_' + #distributionSite")
    public List<Banner> getByDistributionSite(Integer distributionSite) {
        List<Banner> bannerList = bannerMapper.getByDistributionSite(distributionSite);
        return bannerList;
    }
}
